/* cl.ext.ladda 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery',"Ladda","spin"], function($,ladda) {
                factory($,ladda);
            });
        } else if (typeof module === 'object' && module.exports) {
            var spin = require('spin');
            var ladda = require('Ladda');
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery,ladda,spin);
            };
        } else {
            factory(jQuery,Ladda);
        }
    }(
    function ($,Ladda){
        var defaultExtInfo={isBind:true};
        var defaults={
            infoExt:defaultExtInfo
        };
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                return $.ExtCommon.initMergeJson(this.getDefaults(),options);
            }
        };
        var LaddaExtModal = function(element,options) {
            this.element = $(element);
            this.optionBase=$modalCommon.getOptions(options);
            this.objectLadda=Ladda.create(this.element[0]);
            if(this.optionBase.infoExt.isBind){
                this.objectLadda.start();
            }
        };
        LaddaExtModal.prototype = {
            start:function () {
                this.objectLadda.start()
            },
            stop:function () {
                this.objectLadda.stop();
            }
        };
        $.fn.ExtLadda = function(options) {
            var dataFun;
            this.each(function() {
                var el = $(this);
                var tempData=el.data('ExtLadda');
                if (tempData) {
                    if (typeof  options == "string") {
                        if (tempData[options]) {
                            dataFun =tempData[options]()
                        } else {
                            dataFun = tempData
                        }
                    } else {
                        dataFun = tempData
                    }
                } else {
                    dataFun = new LaddaExtModal(el, options);
                    el.data('ExtLadda', dataFun);
                }
            });
            return dataFun;
        };
        return LaddaExtModal;
    })
);
